package com.bestcem.xm.award.dao.impl.mysql;

import com.bestcem.xm.award.controller.param.point.PointAwardWinListParam;
import com.bestcem.xm.award.dao.AwardPointRecordDao;
import com.bestcem.xm.award.dao.mapper.AwardPointRecordMapper;
import com.bestcem.xm.award.entity.mysql.AwardPointRecordEntity;
import com.bestcem.xm.award.entity.pojo.AwardPointRecordDO;
import com.bestcem.xm.award.entity.pojo.PointAwardStatisDO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/6/29 16:57
 * @desc history记录表查询
 */
@Slf4j
@Repository
public class AwardPointRecordDaoImpl implements AwardPointRecordDao {

    @Resource
    AwardPointRecordMapper awardPointRecordMapper;

//    @Override
//    public int insert(AwardPointRecordDO awardPointRecordDO) {
//        if (awardPointRecordDO == null) {
//            return 0;
//        }
//        AwardPointRecordEntity entity = new AwardPointRecordEntity();
//        BeanUtils.copyProperties(awardPointRecordDO, entity);
//        return awardPointRecordMapper.insert(entity);
//    }

    @Override
    public int insertSelective(AwardPointRecordDO awardPointRecordDO) {
        if (awardPointRecordDO == null) {
            return 0;
        }
        AwardPointRecordEntity entity = new AwardPointRecordEntity();
        BeanUtils.copyProperties(awardPointRecordDO, entity);
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        return awardPointRecordMapper.insertSelective(entity);
    }

    @Override
    public long countByIdAndPhoneAndNotPass(String awardId, String phone) {
        Long count = awardPointRecordMapper.countByIdAndPhoneAndNotPass(awardId, phone);
        return count == null ? 0 : count;
    }

    @Override
    public AwardPointRecordDO findOneByRequestId(String requestId) {
        AwardPointRecordEntity entity = awardPointRecordMapper.findOneByRequestId(requestId);
        if (entity == null) {
            return null;
        }
        AwardPointRecordDO awardPointRecordDO = new AwardPointRecordDO();
        BeanUtils.copyProperties(entity, awardPointRecordDO);
        return awardPointRecordDO;
    }

    @Override
    public PointAwardStatisDO getSendAmountAndCount(String awardId) {
        return awardPointRecordMapper.getSendAmountAndCount(awardId);
    }

    @Override
    public List<AwardPointRecordDO> findListByParam(PointAwardWinListParam param) {
        List<AwardPointRecordEntity> entityList = awardPointRecordMapper.findListByParam(param);
        List<AwardPointRecordDO> doList = new ArrayList<>();
        for (AwardPointRecordEntity entity : entityList) {
            if (entity != null) {
                AwardPointRecordDO awardPointRecordDO = new AwardPointRecordDO();
                BeanUtils.copyProperties(entity, awardPointRecordDO);
                doList.add(awardPointRecordDO);
            }
        }
        return doList;
    }

    @Override
    public Long countByParam(PointAwardWinListParam param) {
        Long count = awardPointRecordMapper.countByParam(param);
        return count == null ? 0 : count;
    }
}
